// studentSessionEJB.java
// Stateless Session bean

package studentSession;

import java.rmi.RemoteException;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Vector;

import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.FinderException;
import javax.ejb.RemoveException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.InitialContext;

import offer.Offer;
import offer.OfferHome;
import query.Query;
import query.QueryHome;
import recruiter.Recruiter;
import recruiter.RecruiterHome;
import sector.Sector;
import sector.SectorHome;
import servlets.Notification;
import servlets.WriteReadFile;
import student.Student;
import student.StudentHome;
import timetable.Timetable;
import timetable.TimetableHome;
import academicSup.AcademicSup;
import academicSup.AcademicSupHome;
import city.City;
import city.CityHome;

import company.Company;
import company.CompanyHome;

import country.Country;
import country.CountryHome;
import cv.CV;
import cv.CVHome;
import descriptor.AcademicSupDescriptor;
import descriptor.AcademicSupListDescriptor;
import descriptor.CVDescriptor;
import descriptor.CVListDescriptor;
import descriptor.CitiesListDescriptor;
import descriptor.CompaniesListDescriptor;
import descriptor.CompanyDescriptor;
import descriptor.CompanySupDescriptor;
import descriptor.CountriesListDescriptor;
import descriptor.OfferListDescriptor;
import descriptor.QueryListDescriptor;
import descriptor.SectorsListDescriptor;
import descriptor.TimetableDescriptor;
import descriptor.TimetableListDescriptor;

public class StudentSessionBean implements SessionBean 
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	boolean logged = false;
	StudentHome studentHome = null;
	Student student = null;
	CVHome cvHome = null;
	CV cv = null;
	Company company = null;
	CompanyHome companyHome = null;
	AcademicSup academicSup = null;
	AcademicSupHome academicSupHome = null;
	OfferHome offerHome = null;
	Offer offer = null;
	QueryHome queryHome = null;
	Query query = null;
	RecruiterHome recruiterHome = null;
	Recruiter recruiter = null;
	TimetableHome timetableHome = null;
	Timetable timetable = null;
	Sector sector = null;
	SectorHome sectorHome = null;
	City city =null;
	CityHome cityHome = null;
	Country country = null;
	CountryHome countryHome = null;
	InitialContext initialContext = null;
    SessionContext ejbContext = null;

    // ------------------------------------------------------------------
    // SessionBean implementation
    // ------------------------------------------------------------------
    public void setSessionContext(SessionContext ctx) 
    {
        ejbContext = ctx;
    }
    public void ejbCreate(int id) throws CreateException, RemoteException
    {
    	try 
    	{
			initialContext = new InitialContext();
			
			studentHome = (StudentHome)initialContext.lookup("MyStudent_L");
			student = studentHome.findByPrimaryKey(id);
			
			cvHome = (CVHome)initialContext.lookup("MyCV_L");
			companyHome = (CompanyHome)initialContext.lookup("MyCompany_L");
			academicSupHome = (AcademicSupHome)initialContext.lookup("MyAcademicSup_L");
			offerHome = (OfferHome)initialContext.lookup("MyOffer_L");
			queryHome = (QueryHome)initialContext.lookup("MyQuery_L");
			recruiterHome = (RecruiterHome)initialContext.lookup("MyRecruiter_L");
			timetableHome = (TimetableHome)initialContext.lookup("MyTimetable_L");
			sectorHome = (SectorHome)initialContext.lookup("MySector_L");
			cityHome = (CityHome)initialContext.lookup("MyCity_L");
			countryHome = (CountryHome)initialContext.lookup("MyCountry_L");
		} 
    	catch(Exception e){e.printStackTrace();}
    	setLogged();
    }   
    
    public void ejbRemove() {}
    public void ejbPassivate() {}
    public void ejbActivate() {}
    
    // ------------------------------------------------------------------
    // studentSession implementation
    // ------------------------------------------------------------------
    public boolean isLogged() throws RemoteException
    {
    	return logged;
    }
    
	public void setLogged() throws RemoteException
	{
		this.logged = true;
	}
	
	public String getName() throws RemoteException
	{
		return student.getName();
	}
	
	public String getFirstname() throws RemoteException
	{
		return student.getFirstName();
	}
	
	public String getLogin() throws RemoteException
	{
		return student.getLogin();
	}
	
	public String getDegree() throws RemoteException
	{
		return student.getDegree().getName();
	}
	
	public String getMail() throws RemoteException
	{
		return student.getMail();
	}
    
	public String getPassword() throws RemoteException
	{
		return student.getPasswd();
	}
	
	public AcademicSupDescriptor getAcademicSup() throws RemoteException
	{
		if(student.getAcademicSup() != null)
		{
			return student.getAcademicSup().getDescriptor();
		}
		else
		{
			return null;
		}
	}
	
	public CompanySupDescriptor getCompanySup() throws RemoteException
	{
		if(student.getCompanySup() != null)
		{
			return student.getCompanySup().toSimpleCompanySupDescriptor();
		}
		else
		{
			return null;
		}
	}
	
	public void updateMail(String email) throws RemoteException
	{
		student.setMail(email);
	}
	
	public void updatePasswd(String passwd) throws RemoteException
	{
		student.setPasswd(passwd);
	}
	
	public CVListDescriptor viewCVs() throws RemoteException
	{
		CVListDescriptor listCV = new CVListDescriptor();
		Collection<CV> cvs = student.getCVs();
		for (CV o:cvs)
		{
			listCV.addCVDescriptor(((CV)o).getDescriptor());
		}
		return listCV;		
	}
	
	public CVDescriptor viewCV(int CVid) throws RemoteException
	{
		CVDescriptor desc = null;
		CV cvObject = null;
		try 
		{
			cvObject = cvHome.findByPrimaryKey(CVid);
		} 
		catch (FinderException e) 
		{
				e.printStackTrace();
		}	
		if(cvObject != null) 
		{
			desc = cvObject.getDescriptor();
		}
		return desc;
	}
	
	public boolean uploadCV(String fileName,String fileLocation,boolean auth) throws RemoteException
	{
		boolean uploaded = false;
		try 
		{
			cvHome.create(fileName,fileLocation,auth,student);	
			uploaded = true;
		} 
		catch (CreateException e) 
		{
			e.printStackTrace();
		}
		return uploaded;
	}
	
	public boolean deleteCV(int CVid) throws RemoteException
	{
		boolean deleted = false;
		CV cvObject = null;
		try 
		{
			cvObject = cvHome.findByPrimaryKey(CVid);
		} 
		catch (FinderException e) 
		{
				e.printStackTrace();
		}	
		if(cvObject != null) 
		{
			try 
			{
				cvObject.remove();
			}
			catch (EJBException e) {e.printStackTrace();} 
			catch (RemoveException e) {e.printStackTrace();}
			deleted = true;
		}
		return deleted;
		
	}
	
	public boolean updateCV(int CVid,boolean auth) throws RemoteException
	{
		boolean updated = false;
		CV cvOjbect = null;
		try 
		{
			cvOjbect = cvHome.findByPrimaryKey(CVid);
		} 
		catch (FinderException e) 
		{
				e.printStackTrace();
		}	
		if(cvOjbect != null) 
		{
			cvOjbect.setPublishAuth(auth);
			updated = true;
		}
		return updated;
	}
	
	public CompaniesListDescriptor getAllCompanies() throws RemoteException
	{
		CompaniesListDescriptor listComp = new CompaniesListDescriptor();
		Collection<Company> comp = null;
		try 
		{
			comp = companyHome.findAllCompanies();
		} 
		catch (FinderException e) 
		{
			e.printStackTrace();
		}
		for (Object o:comp)
		{
			listComp.addCompanyDescriptor(((Company)o).getDescriptor());
		}
		return listComp;
	}
	
	public CompanyDescriptor getCompany(int id) throws RemoteException
	{
		CompanyDescriptor compDes = new CompanyDescriptor();
		Company comp = null;
		try 
		{
			comp = companyHome.findByPrimaryKey(id);
		} 
		catch (FinderException e) 
		{
			e.printStackTrace();
		}
		if(comp != null)
		{
			compDes = comp.getDescriptor();
		}
		return compDes;
	}
	
	public AcademicSupListDescriptor getAllAcademicSup() throws RemoteException
	{
		AcademicSupListDescriptor listAcademicSup = new AcademicSupListDescriptor();
		Collection<AcademicSup> academicSup = null;
		try 
		{
			academicSup = academicSupHome.findAllAcademicSup();
		} 
		catch (FinderException e) 
		{
			e.printStackTrace();
		}
		for (Object o:academicSup)
		{
			listAcademicSup.addAcademicSupDescriptor(((AcademicSup)o).getDescriptor());
		}
		return listAcademicSup;
	}
	
	public AcademicSupListDescriptor getAcademicSup(String dep) throws RemoteException
	{
		AcademicSupListDescriptor listAcademicSup = new AcademicSupListDescriptor();
		Collection<AcademicSup> academicSup = null;
		try 
		{
			academicSup = academicSupHome.findAllAcademicSup();
		} 
		catch (FinderException e) 
		{
			e.printStackTrace();
		}
		for (AcademicSup o:academicSup)
		{
			if(o.getDepartment().getName().equals(dep))
			{
				listAcademicSup.addAcademicSupDescriptor((o).getDescriptor());
			}
		}
		return listAcademicSup;
	}
	
	
	public OfferListDescriptor viewAllOffers() throws RemoteException
	{
		OfferListDescriptor offerList = new OfferListDescriptor();
		Collection<Offer> offersColl = null;
		try 
		{
			offersColl = offerHome.findAllOffers();
		} 
		catch (FinderException e) 
		{
			e.printStackTrace();
		}
		for (Offer o:offersColl)
		{
			if(o.getType() == 1)
			{
				offerList.addOffer((o).getDescriptor());
			}
		}
		return offerList;
	}
	
	public OfferListDescriptor viewAllOffers2() throws RemoteException
	{
		OfferListDescriptor offerList = new OfferListDescriptor();
		CompaniesListDescriptor compList = viewCompaniesPref();
		Company comp = null;
		if(compList.sizeCompaniesList() > 0)
		{
			for(int i=0 ; i<compList.sizeCompaniesList() ; i++)
			{
				try 
				{
					comp = companyHome.findByPrimaryKey(compList.getCompanyDescriptors().get(i).getId());
				} 
				catch (FinderException e) {e.printStackTrace();}
				if(comp != null)
				{
					for(Offer o:comp.getOffers())
					{
						if(o.getType() == 1)
							offerList.addOffer(o.getDescriptor());
					}
				}
			}
		}
		return offerList;
	}
	
	public OfferListDescriptor viewPersonalOffers() throws RemoteException
	{
		OfferListDescriptor offerList = new OfferListDescriptor();
		for (Offer o:student.getOffers())
		{
			offerList.addOffer((o).getDescriptor());
		}
		return offerList;
	}

	public boolean acceptOffer(int id) throws RemoteException
	{
		boolean added = false;
		Offer offer = null;
		try 
		{
			offer = offerHome.findByPrimaryKey(id);
		} 
		catch (FinderException e) {e.printStackTrace();}
		if(offer != null)
		{
			if(offer.getStudent() == null && offer.getType() == 1 && offer.getStatus() == Offer.PUBLISH)
			{
				added = student.getOffers().add(offer);
				CompaniesListDescriptor compList = viewCompaniesPref();
				boolean addCompPref = true;
				for(int i=0 ; i<compList.sizeCompaniesList() ; i++)
				{
					if(offer.getCompany().getId() == compList.getCompanyDescriptors().get(i).getId())
					{
						addCompPref = false;
					}
				}
				if(addCompPref)
					setCompanyPref(offer.getCompany().getId());
				if(offer.getType() == 0)
					offer.setStatus(Offer.ACCEPTED);
			}
			if(offer.getStudent() == student)
			{
				if(offer.getStatus() == Offer.OK_WORPLACEMENT_COMP)
				{
					offer.setStatus(Offer.OK_WORPLACEMENT_STUDENT);
					if(offer.getCompanySup() != null)
					{
						student.setCompanySup(offer.getCompanySup());
						added = true;
					}
				}
				if(offer.getStatus() == Offer.PUBLISH && offer.getType() == 0)
				{
					offer.setStatus(Offer.INTERVIEW);
					added = true;
				}
			}
			
			
		}
		return added;
	}
	
	public boolean rejectOffer(int id) throws RemoteException
	{
		boolean rejected = false;
		Offer offer = null;
		try 
		{
			offer = offerHome.findByPrimaryKey(id);
		} 
		catch (FinderException e) {e.printStackTrace();}
		if(offer != null)
		{
			if(offer.getStudent() == student)
			{
				offer.setStatus(Offer.REJECTED);
				rejected = student.getOffers().remove(offer);
			}
		}
		return rejected;
	}
	
	public void editDiary(String newline)
	{
		String filelocation = null;
		filelocation = student.getPlacementDiary().getContent();
		WriteReadFile.writeDiary(newline,Notification.DiaryDir,filelocation);
	}
	
	public String viewDiary1() throws RemoteException
	{
		String filelocation = "";
		String content = null;
		filelocation = student.getPlacementDiary().getContent();
		content = WriteReadFile.read(Notification.DiaryDir+filelocation);
		content = content.replaceAll("(.{3}?.+?:)", "<h4>$1</h4>");
		content = content.replaceAll("\n", "<br>");
		return content;
	}
	
	public String viewDiary2() throws RemoteException
	{
		String filelocation = "";
		String content = null;
		filelocation = student.getPlacementDiary().getContent();
		content = WriteReadFile.read(Notification.DiaryDir+filelocation);
		content = content.replaceAll("\n", "");
		return content;
	}
	
	public QueryListDescriptor viewAllQuery() throws RemoteException
	{
		QueryListDescriptor queryList = new QueryListDescriptor();
		for (Query o:student.getQueries())
		{
			queryList.addQuery((o).getDescriptor());
		}
		return queryList;
		
	}
	
	public boolean sendQuery(int academicSupID, String message, int offerID) throws RemoteException
	{
		boolean sent = false;
		Offer offer = null;
		AcademicSup academicSup = null;
		try 
		{
			if(academicSupID != 0)
			{
				academicSup = academicSupHome.findByPrimaryKey(academicSupID);
			}
			if(offerID != 0)
			{
				offer = offerHome.findByPrimaryKey(offerID);
			}
		} 
		catch (FinderException e) {e.printStackTrace();}
		if(academicSup != null)
		{
			try 
			{
				query = queryHome.create(message, student, academicSup);
			} 
			catch (CreateException e) {e.printStackTrace();} 
			if(offer != null)
			{
				query.setOffer(offer);
			}
		}
		if(query != null)
		{
			sent = true;
		}
		return sent;
	}
	
	public boolean deleteQuery(int queryID) throws RemoteException
	{
		boolean deleted = false;
		try 
		{
			query = queryHome.findByPrimaryKey(queryID);
		} 
		catch (FinderException e) {e.printStackTrace();}
		if(query != null)
		{
			if(query.getStatus() == -1)
				deleted = student.getQueries().remove(query);
		}
		return deleted;
	}
	
	public TimetableListDescriptor getAllEvents() throws RemoteException
	{
		TimetableListDescriptor listEvents = new TimetableListDescriptor();
		Collection<Timetable> tb = student.getTimetables();
		for (Object o:tb)
		{
			listEvents.addTimetableDescriptor(((Timetable)o).getDescriptor());
		}
		return listEvents;
	}
	
	public TimetableListDescriptor getEvents(int day, int month, int year) throws RemoteException
	{
		TimetableListDescriptor listEvents = new TimetableListDescriptor();
		Collection<Timetable> tb = null;
		try 
		{
			tb = timetableHome.findByDayMonthYear(day, month, year);
		} 
		catch (FinderException e) {e.printStackTrace();}
		for (Object o:tb)
		{
			listEvents.addTimetableDescriptor(((Timetable)o).getDescriptor());
		}
		return listEvents;
	}
	
	public boolean addEvent(String name,int day,int month, int year,int hour, int minute,String recruiterName, String offerName) throws RemoteException
	{
		boolean added = false;
		Timetable timetableObject = null;
		try 
		{
			timetableObject = timetableHome.create(name,day,month,year,hour,minute,student);
			timetableObject.setRecruiterName(recruiterName);
			timetableObject.setOfferName(offerName);
			added = true;
		} 
		catch (CreateException e) {e.printStackTrace();} 
		return added;
	}
	
	public boolean addMultiEvent(String name,int day1,int month1, int year1,int day2,int month2, int year2) throws RemoteException
	{
		boolean added = false;
		Timetable timetableObject = null;
		String date1 = month1+"/"+day1+"/"+year1;
		String date2 = month2+"/"+day2+"/"+year2;
		DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
		df.setLenient(false);  // this is important!
        Date dt1 = null;
        Date dt2 = null;
		try 
		{
			dt1 = df.parse(date1);
			dt2 = df.parse(date2);
		} 
		catch (ParseException e1) {e1.printStackTrace();}
        
        GregorianCalendar calendar1 = new GregorianCalendar(); 
		calendar1.setTime(dt1);
		GregorianCalendar calendar2 = new GregorianCalendar(); 
		calendar2.setTime(dt2);
        while(calendar1.compareTo(calendar2) <= 0)
        {
        	try 
    		{
        		int month = calendar1.get(GregorianCalendar.MONTH) + 1;
        		timetableObject = timetableHome.create(name,calendar1.get(GregorianCalendar.DATE),month,calendar1.get(GregorianCalendar.YEAR),9,30,student);
        		timetableObject.setRecruiterName("...");
    			timetableObject.setOfferName("...");
        		added = true;
    		} 
    		catch (CreateException e) {e.printStackTrace();} 
    		calendar1.add(Calendar.DATE ,1);
        }
		return added;
	}
	
	public boolean deleteEvent(int eventID) throws RemoteException
	{
		boolean deleted = false;
		Timetable timetableObject = null;
		try 
		{
			timetableObject = timetableHome.findByPrimaryKey(eventID);
			timetableObject.remove();
			deleted = true;
		} 
		catch (FinderException e) {e.printStackTrace();} 
		catch (EJBException e) {e.printStackTrace();} 
		catch (RemoveException e) {e.printStackTrace();} 
		return deleted;
	}
	
	public TimetableDescriptor editEvent(int eventID) throws RemoteException
	{
		Timetable timetableObject = null;
		TimetableDescriptor desc = null;
		try 
		{
			timetableObject = timetableHome.findByPrimaryKey(eventID);
			desc = timetableObject.getDescriptor();
		} 
		catch (FinderException e) {e.printStackTrace();}
		return desc;
	}
	
	public boolean editedEvent(int eventID,String name,int day,int month,int year,int hour, int minute, String recruiterName, String offerName) throws RemoteException
	{
		boolean edited = false;
		Timetable timetableObject = null;
		try 
		{
			timetableObject = timetableHome.findByPrimaryKey(eventID);
			timetableObject.setName(name);
			timetableObject.setDay(day);
			timetableObject.setMonth(month);
			timetableObject.setYear(year);
			timetableObject.setHour(hour);
			timetableObject.setMinute(minute);
			timetableObject.setRecruiterName(recruiterName);
			timetableObject.setOfferName(offerName);
			edited = true;
		} 
		catch (FinderException e) {e.printStackTrace();}
		return edited;
	}
	
	public CompaniesListDescriptor viewCompaniesPref() throws RemoteException
	{
		CompaniesListDescriptor listComp = new CompaniesListDescriptor();
		Collection<Company> compPref = null;
		compPref = student.getCompaniesPref();
		
		for (Object o:compPref)
		{
			listComp.addCompanyDescriptor(((Company)o).getDescriptor());
		}
		return listComp;
	}
	
	public boolean setCompanyPref(int id) throws RemoteException
	{
		Company comp = null;
		try 
		{
			comp = companyHome.findByPrimaryKey(id);
		} 
		catch (FinderException e) {e.printStackTrace();}
		/*boolean add = true;
		Collection<Company> compColl = student.getCompaniesPref();
		deleteCompanyPref2();
		for(Company c:compColl)
		{
			if(c.getId() == comp.getId())
			{
				add = false;
			}
		}*/
		//if(add)
		//{
			//compColl.add(comp);
			//student.setCompaniesPref(compColl);
			student.getCompaniesPref().add(comp);
			comp.getInterestedStudents().add(student);
		//}
		return true;
	}
	
	public boolean deleteCompanyPref(int id) throws RemoteException
	{
		Company comp = null;
		try 
		{
			comp = companyHome.findByPrimaryKey(id);
		} 
		catch (FinderException e) {e.printStackTrace();}
		/*boolean delete = true;
		Collection<Company> compColl = student.getCompaniesPref();
		deleteCompanyPref2();
		for(Company c:compColl)
		{
			if(c.getId() == comp.getId())
			{
				delete = false;
			}
		}
		if(delete)
		{
			compColl.remove(comp);
			deleteCompanyPref2();
			student.setCompaniesPref(compColl);*/
			student.getCompaniesPref().remove(comp);
			comp.getInterestedStudents().remove(student);
		//}
		return true;
	}
	
	public boolean deleteCompanyPref2() throws RemoteException
	{
		boolean deleted = false;
		student.setCompaniesPref(new Vector<Company>());
		if(student.getCompaniesPref().size() == 0)
		{
			deleted = true;
		}
		return deleted;
	}
	
	public SectorsListDescriptor viewSectorsPref() throws RemoteException
	{
		SectorsListDescriptor listSec = new SectorsListDescriptor();
		Collection<Sector> secPref = null;
		secPref = student.getSectorsPref();
		
		for (Object o:secPref)
		{
			listSec.addSectorDescriptor(((Sector)o).getDescriptor());
		}
		return listSec;
	}
	
	public boolean setSectorPref(Vector<Integer> vecID) throws RemoteException
	{
		Sector sct = null;
		boolean add = true;
		deleteSectorPref();
		for(int i=0 ; i<vecID.size() ; i++)
		{
			try 
			{
				sct = sectorHome.findByPrimaryKey(vecID.elementAt(i));
			} 
			catch (FinderException e) 
			{
				e.printStackTrace(); 
				add = false;
			}
			student.getSectorsPref().add(sct);
		}
		return add;
	}
	
	public boolean deleteSectorPref() throws RemoteException
	{
		boolean deleted = false;
		student.setSectorsPref(new Vector<Sector>());
		if(student.getSectorsPref().size() == 0)
		{
			deleted = true;
		}
		return deleted;
	}
	
	public CitiesListDescriptor viewCitiesPref() throws RemoteException
	{
		CitiesListDescriptor listCities = new CitiesListDescriptor();
		Collection<City> cityPref = null;
		cityPref = student.getCitiesPref();
		
		for (Object o:cityPref)
		{
			listCities.addCityDescriptor(((City)o).getDescriptor());
		}
		return listCities;
	}
	
	public boolean setCityPref(Vector<Integer> vecID) throws RemoteException
	{
		City ct = null;
		boolean add = true;
		deleteCityPref();
		for(int i=0 ; i<vecID.size() ; i++)
		{
			try 
			{
				ct = cityHome.findByPrimaryKey(vecID.elementAt(i));
			} 
			catch (FinderException e) 
			{
				e.printStackTrace(); 
				add = false;
			}
			student.getCitiesPref().add(ct);
		}
		return add;
	}
	
	public boolean deleteCityPref() throws RemoteException
	{
		boolean deleted = false;
		student.setCitiesPref(new Vector<City>());
		if(student.getCitiesPref().size() == 0)
		{
			deleted = true;
		}
		return deleted;
	}
	
	public CountriesListDescriptor viewCountriesPref() throws RemoteException
	{
		CountriesListDescriptor listcountries= new CountriesListDescriptor();
		Collection<Country> countryPref = null;
		countryPref = student.getCountriesPref();
		
		for (Object o:countryPref)
		{
			listcountries.addCountryDescriptor(((Country)o).getDescriptor());
		}
		return listcountries;
	}
	
	public boolean setCountryPref(Vector<Integer> vecID) throws RemoteException
	{
		Country ctr = null;
		boolean add = true;
		deleteCountryPref();
		for(int i=0 ; i<vecID.size() ; i++)
		{
			try 
			{
				ctr = countryHome.findByPrimaryKey(vecID.elementAt(i));
			} 
			catch (FinderException e) 
			{
				e.printStackTrace(); 
				add = false;
			}
			student.getCountriesPref().add(ctr);
		}
		return add;
	}
	
	public boolean deleteCountryPref() throws RemoteException
	{
		boolean deleted = false;
		student.setCountriesPref(new Vector<Country>());
		if(student.getCountriesPref().size() == 0)
		{
			deleted = true;
		}
		return deleted;
	}
	
	public SectorsListDescriptor getSectors() throws RemoteException
	{
		SectorsListDescriptor listSec = new SectorsListDescriptor();
		Collection<Sector> secPref = null;
		try 
		{
			secPref = sectorHome.findAllSectors();
		} 
		catch (FinderException e) {e.printStackTrace();}
		for (Object o:secPref)
		{
			listSec.addSectorDescriptor(((Sector)o).getDescriptor());
		}
		return listSec;
	}
	
	public CitiesListDescriptor getCities() throws RemoteException
	{
		CitiesListDescriptor listCities = new CitiesListDescriptor();
		Collection<City> cityPref = null;
		try 
		{
			cityPref = cityHome.findAllCities();
		} 
		catch (FinderException e) {e.printStackTrace();}
		for (Object o:cityPref)
		{
			listCities.addCityDescriptor(((City)o).getDescriptor());
		}
		return listCities;
	}
	
	public CountriesListDescriptor getCountries() throws RemoteException
	{
		CountriesListDescriptor listcountries= new CountriesListDescriptor();
		Collection<Country> countryPref = null;
		try 
		{
			countryPref = countryHome.findAllCountries();
		} 
		catch (FinderException e) {e.printStackTrace();}
		for (Object o:countryPref)
		{
			listcountries.addCountryDescriptor(((Country)o).getDescriptor());
		}
		return listcountries;
	}
	
	public CompaniesListDescriptor getCompanies() throws RemoteException
	{
		SectorsListDescriptor listSecPref = new SectorsListDescriptor();
		listSecPref = viewSectorsPref();
		CitiesListDescriptor listCitiesPref = new CitiesListDescriptor();
		listCitiesPref = viewCitiesPref();
		CountriesListDescriptor listcountriesPref= new CountriesListDescriptor();
		listcountriesPref = viewCountriesPref();
		CompaniesListDescriptor listCompPref = new CompaniesListDescriptor();
		CompaniesListDescriptor listComp = new CompaniesListDescriptor();
		listComp = getAllCompanies();
		for(int j=0 ; j<listSecPref.sizeSectorList() ; j++)
		{
			for(int i=0 ; i< listComp.sizeCompaniesList() ; i++)
			{
				if(listComp.getCompanyDescriptors().get(i).getSector().equals(listSecPref.getSectorDescriptors().get(j).getName()))
				{
					listCompPref.addCompanyDescriptor(listComp.getCompanyDescriptors().get(i));
					listComp.getCompanyDescriptors().remove(i);
				}
			}
		}
		
		for(int j=0 ; j<listCitiesPref.sizeCitiesList() ; j++)
		{
			for(int i=0 ; i<listComp.sizeCompaniesList() ; i++)
			{
				if(listComp.getCompanyDescriptors().get(i).getCity().equals(listCitiesPref.getCityDescriptors().get(j).getName()))
				{
					listCompPref.addCompanyDescriptor(listComp.getCompanyDescriptors().get(i));
					listComp.getCompanyDescriptors().remove(i);
				}
			}
		}
		for(int j=0 ; j<listcountriesPref.sizeCountriesList() ; j++)
		{
			for(int i=0 ; i< listComp.sizeCompaniesList() ; i++)
			{
				if(listComp.getCompanyDescriptors().get(i).getCountry().equals(listcountriesPref.getCountryDescriptors().get(j).getName()))
				{
					listCompPref.addCompanyDescriptor(listComp.getCompanyDescriptors().get(i));
					listComp.getCompanyDescriptors().remove(i);
				}
			}
		}
		return listCompPref;

	}
}

